﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace HisGL.CA.NetCA
{
  public class ResNetCA
  {
    /// <summary>
    /// 响应结构头部
    /// </summary>
    public class ResponseResult
    {
      [JsonProperty(PropertyName = "status")]
      public int Status { get; set; }

      [JsonProperty(PropertyName = "msg")]
      public string Message { get; set; }

      [JsonIgnore]
      public bool Success { get { return 0 == Status; } }
    }

    /// <summary>
    /// 基础响应
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ResponseBase<T>
    {
      [JsonProperty(PropertyName = "responseResult")]
      public ResponseResult ResponseResult { get; set; }
      [JsonProperty(PropertyName = "contents")]
      public T Contents { get; set; }
      [JsonIgnore]
      public virtual bool IsValid { get { return true; } }
    }

    public class BasicResponse : ResponseBase<object>
    {
      public override bool IsValid { get { return ResponseResult.Success; } }
    }

    #region 查询证书
    public class QueryCertResponse : ResponseBase<Dictionary<string, List<string>>>
    {
      public override bool IsValid { get { return null != Contents && Contents.Count > 0; } }
    }
    #endregion

    #region 查询已授权信息
    public class AuthorizedUserInfo
    {
      [JsonProperty(PropertyName = "id")]
      public int Id { get; set; }
      [JsonProperty(PropertyName = "name")]
      public string Name { get; set; }
    }
    public class QueryAuthorizedUserContents
    {
      [JsonProperty(PropertyName = "total")]
      public int Total { get; set; }
      [JsonProperty(PropertyName = "currentRowsSize")]
      public int CurrentRowsSize { get; set; }
      [JsonProperty(PropertyName = "rows")]
      public List<AuthorizedUserInfo> Rows { get; set; }
    }
    public class QueryAuthorizedUserResponse : ResponseBase<QueryAuthorizedUserContents>
    {
      public override bool IsValid { get { return null != Contents && Contents.Total > 0; } }
    }
    #endregion


    #region 登录
    public class LoginContent
    {
      [JsonProperty(PropertyName = "userToken")]
      public string UserToken { get; set; }
    }
    public class LoginResponse : ResponseBase<LoginContent>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.UserToken); } }
    }

    public class SmsContent
    {
      [JsonProperty(PropertyName = "smsCodeGenMinCycle")]
      public int SmsCodeGenMinCycle { get; set; }
    }
    public class SmsResponse : ResponseBase<SmsContent>
    {
      public override bool IsValid { get { return true; } }
    }
    #endregion

    #region 密码重试次数
    public class QueryPasswordRetryCountResponse : ResponseBase<int>
    {
      public override bool IsValid { get { return Contents > 0; } }
    }
    #endregion


    #region 扫码认证
    public class QrCodeContent
    {
      [JsonProperty(PropertyName = "qrCodeBase64")]
      public string QrCodeBase64 { get; set; }
      [JsonProperty(PropertyName = "qrCodeIdentity")]
      public string QrCodeIdentity { get; set; }
      [JsonProperty(PropertyName = "qrCodeExpireDate")]
      public int QrCodeExpireDate { get; set; }
    }
    public class QrCodeStatusContent
    {
      [JsonProperty(PropertyName = "verifyStatus")]
      public int VerifyStatus { get; set; }
      [JsonProperty(PropertyName = "userToken")]
      public string UserToken { get; set; }
      [JsonProperty(PropertyName = "errorMsg")]
      public string ErrorMessage { get; set; }
    }
    public class QrCode
    {
      [JsonProperty(PropertyName = "type")]
      public int Type { get; set; }
      [JsonProperty(PropertyName = "qrCodeBase64")]
      public string QrCodeBase64 { get; set; }
      [JsonProperty(PropertyName = "qrCodeContent")]
      public string QrCodeContent { get; set; }
    };
    public class QrCodeNoStatusContent
    {
      [JsonProperty(PropertyName = "qrCodes")]
      public List<QrCode> QrCodes { get; set; }
      [JsonProperty(PropertyName = "qrCodeIdentity")]
      public string QrCodeIdentity { get; set; }
      [JsonProperty(PropertyName = "qrCodeExpireDate")]
      public int QrCodeExpireDate { get; set; }
    }
    public class GenerateQrCodeResponse : ResponseBase<QrCodeContent>
    {
      public override bool IsValid
      {
        get
        {
          return
              null != Contents &&
              !string.IsNullOrEmpty(Contents.QrCodeBase64) &&
              !string.IsNullOrEmpty(Contents.QrCodeIdentity);
        }
      }
    }
    public class GenerateNoStatusQrCodeResponse : ResponseBase<QrCodeNoStatusContent>
    {
      public override bool IsValid
      {
        get
        {
          return
              null != Contents &&
              Contents.QrCodes.Count > 0 &&
              !string.IsNullOrEmpty(Contents.QrCodeIdentity);
        }
      }
    }
    public class QueryQrCodeStatusResponse : ResponseBase<QrCodeStatusContent>
    {
      public override bool IsValid
      {
        get
        {
          return
              null != Contents &&
              Contents.VerifyStatus == 0 &&
              !string.IsNullOrEmpty(Contents.UserToken);
        }
      }
    }
    #endregion


    #region 签名
    public class SignResult
    {
      [JsonProperty(PropertyName = "signedData")]
      public string SignedData { get; set; }
    }
    public class SignResponse : ResponseBase<SignResult>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.SignedData); } }
    }
    #endregion


    #region 下载签章图片
    public class DownloadSealContent
    {
      [JsonProperty(PropertyName = "picBase64")]
      public string SealImageBase64 { get; set; }
    }
    public class DownloadSealResponse : ResponseBase<DownloadSealContent>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.SealImageBase64); } }
    }
    #endregion

    #region PDF签章
    public class PDFSignSealContent
    {
      [JsonProperty(PropertyName = "sealPdf")]
      public string SignedPDFBase64 { get; set; }
    }
    public class PDFSignSealResponse : ResponseBase<PDFSignSealContent>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.SignedPDFBase64); } }
    }
    #endregion

    #region PDF验证
    public class VerifyPDFSealContent
    {
      [JsonProperty(PropertyName = "globalState")]
      public int GlobalState { get; set; }

      [JsonProperty(PropertyName = "signFieldInfos")]
      public List<SignFieldInfo> SignFieldInfos { get; set; }
    }

    public class SignFieldInfo
    {
      /// <summary>
      /// signFieldName	String	签名域名称
      /// </summary>
      [JsonProperty(PropertyName = "signFieldName")]
      public string SignFieldName { get; set; }

      /// <summary>
      /// certContent	String	签名证书内容
      /// </summary>
      [JsonProperty(PropertyName = "certContent")]
      public string CertContent { get; set; }

      /// <summary>
      /// validity	Validity	验证信息，参见12.1.20Validity结构
      /// </summary>
      [JsonProperty(PropertyName = "signFieldName")]
      public Validity Validity { get; set; }

      /// <summary>
      /// signInfo	SignInfo	签名信息，参见12.1.22SignInfo结构
      /// </summary>
      [JsonProperty(PropertyName = "signInfo")]
      public SignInfo SignInfo { get; set; }
    }

    public class Validity
    {
      /// <summary>
      /// state	Int	该签名域对应的验证状态，0为验证成功，1为验证失败
      /// </summary>
      [JsonProperty(PropertyName = "state")]
      public int State { get; set; }

      /// <summary>
      /// faultReason	FaultReason	state为1时存在，表示验证失败的原因，见12.1.21 FaultReason结构
      /// </summary>
      [JsonProperty(PropertyName = "faultReason")]
      public FaultReason FaultReason { get; set; }

    }

    public class FaultReason
    {
      /// <summary>
      /// code	Int	错误代号
      /// </summary>
      [JsonProperty(PropertyName = "code")]
      public int Code { get; set; }

      /// <summary>
      /// desc	String	错误描述
      /// </summary>
      [JsonProperty(PropertyName = "desc")]
      public string Desc { get; set; }

    }

    public class SignInfo
    {
      /// <summary>
      /// signer	String	签名者名称
      /// </summary>
      [JsonProperty(PropertyName = "signer")]
      public string Signer { get; set; }

      /// <summary>
      /// tsaTime	String	可信时间戳时间
      /// </summary>
      [JsonProperty(PropertyName = "tsaTime")]
      public string TsaTime { get; set; }

      /// <summary>
      /// signingTime	String	签名时间
      /// </summary>
      [JsonProperty(PropertyName = "signingTime")]
      public string SigningTime { get; set; }
    }

    public class VerifyPDFSealResponse : ResponseBase<VerifyPDFSealContent>
    {
      public override bool IsValid { get { return null != Contents && Contents.GlobalState == 0; } }
    }
    #endregion

    #region 操作信息
    public class OprInfo
    {
      /// <summary>
      /// name	String	当前操作者姓名
      /// </summary>
      [JsonProperty(PropertyName = "name")]
      public string Name { get; set; }      
      
      /// <summary>
      /// uid	String	当前操作者工号
      /// </summary>
      [JsonProperty(PropertyName = "uid")]
      public string Uid { get; set; }

      /// <summary>
      /// projectUid	String	云密钥项目uid（项目标识）
      /// </summary>
      [JsonProperty(PropertyName = "projectUid")]
      public string ProjectUid { get; set; }

      /// <summary>
      /// phone	String	当前操作者手机号
      /// </summary>
      [JsonProperty(PropertyName = "phone")]
      public string Phone { get; set; }

      /// <summary>
      /// memo	String	备注信息
      /// </summary>
      [JsonProperty(PropertyName = "memo")]
      public string Memo { get; set; }

      /// <summary>
      /// userType	Int	0表示证书用户 1表示被授权用户
      /// </summary>
      [JsonProperty(PropertyName = "userType")]
      public int UserType { get; set; }

      /// <summary>
      /// authorizedUserId	Int	被授权用户id，userType为0时该值为空
      /// </summary>
      [JsonProperty(PropertyName = "authorizedUserId")]
      public string AuthorizedUserId { get; set; }

      /// <summary>
      /// userType	Int	0表示证书用户 1表示被授权用户
      /// </summary>
      [JsonProperty(PropertyName = "certContentInfo")]
      public CertContentInfo CertContentInfo { get; set; }

    }
    public class OprInfoResponse : ResponseBase<OprInfo>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.Uid); } }
    }
    #endregion

    #region 用户证书信息
    public class CertContentInfo
    {
            
      /// <summary>
      /// signCert	String	签名证书的base64编码
      /// </summary>
      [JsonProperty(PropertyName = "signCert")]
      public string SignCert { get; set; }

      /// <summary>
      /// encCert	String	加密证书的base64编码
      /// </summary>
      [JsonProperty(PropertyName = "encCert")]
      public string EncCert { get; set; }


    }
    public class CertContentInfoResponse : ResponseBase<CertContentInfo>
    {
      public override bool IsValid { get { return true; } }
    }

    public class CertContentRevokeResponse : ResponseBase<string>
    {
      public override bool IsValid { get { return true; } }
    }

    #endregion

    #region 8.1.机构信息注册
    public class OrganizationInfo
    {

      /// <summary>
      /// organizationId	Int	机构注册成功后返回的机构id，唯一
      /// </summary>
      [JsonProperty(PropertyName = "organizationId")]
      public string OrganizationId { get; set; }


    }
    public class OrganizationInfoResponse : ResponseBase<OrganizationInfo>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.OrganizationId); } }
    }
    #endregion

    #region 8.3.用户解锁
    public class UserUnLockInfo
    {

      /// <summary>
      /// phone	String	发送解锁PIN码的目标手机号码
      /// </summary>
      [JsonProperty(PropertyName = "phone")]
      public string Phone { get; set; }

      /// <summary>
      /// userPin	String	根据配置决定是否返回解锁后的PIN码
      /// 若返回，则是一个经过authId对应证书加密过的base64编码的字符串；否则使用短信下发解锁密码
      /// </summary>
      [JsonProperty(PropertyName = "userPin")]
      public string UserPin { get; set; }

    }
    public class UserUnLockInfoResponse : ResponseBase<UserUnLockInfo>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.Phone); } }
    }
    #endregion

    #region 用户证书状态
    public class CertContentStatusInfo
    {

      /// <summary>
      /// certStatus	int	证书状态常量，参考证书状态常量
      /// </summary>
      [JsonProperty(PropertyName = "certStatus")]
      public string CertStatus { get; set; }

      /// <summary>
      /// certStatusDesc	String	证书状态描述，参考证书状态常量
      /// </summary>
      [JsonProperty(PropertyName = "certStatusDesc")]
      public string CertStatusDesc { get; set; }


    }
    public class CertContentStatusInfoResponse : ResponseBase<CertContentStatusInfo>
    {
      public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.CertStatus); } }
    }

    #endregion

    #region 2.6.解析证书属性及扩展
    public class CertContentParseInfo
    {

        /// <summary>
        /// version	Int	版本号，参考证书的版本号
        /// </summary>
        [JsonProperty(PropertyName = "version")]
        public int Version { get; set; }

        /// <summary>
        /// signAlgo	Int	签名算法，参考签名算法
        /// </summary>
        [JsonProperty(PropertyName = "signAlgo")]
        public int SignAlgo { get; set; }


        /// <summary>
        /// publicAlgo	Int	公钥算法，参考公钥算法
        /// </summary>
        [JsonProperty(PropertyName = "publicAlgo")]
        public int PublicAlgo { get; set; }

        /// <summary>
        /// publicBits	Int	公钥位数
        /// </summary>
        [JsonProperty(PropertyName = "publicBits")]
        public int PublicBits { get; set; }

        /// <summary>
        /// keyUsage	Int	密钥用法，参考密钥用法
        /// </summary>
        [JsonProperty(PropertyName = "keyUsage")]
        public int KeyUsage { get; set; }

        /// <summary>
        /// publicEccCurve	Int	公钥的椭圆曲线号，国密算法证书才有,参考ECC曲线的类型
        /// </summary>
        [JsonProperty(PropertyName = "publicEccCurve")]
        public int PublicEccCurve { get; set; }

        /// <summary>
        /// validityStart	String	有效期开始时间
        /// </summary>
        [JsonProperty(PropertyName = "validityStart")]
        public string ValidityStart { get; set; }

        /// <summary>
        /// validityEnd	String	有效期结束时间
        /// </summary>
        [JsonProperty(PropertyName = "validityEnd")]
        public string ValidityEnd { get; set; }

        /// <summary>
        /// sn	String	证书序列号
        /// </summary>
        [JsonProperty(PropertyName = "sn")]
        public string Sn { get; set; }

        /// <summary>
        /// thumbprints	Thumbprint[]	证书摘要，参考12.1.8Thumbprint
        /// </summary>
        [JsonProperty(PropertyName = "thumbprints")]
        public List<Thumbprint> Thumbprints { get; set; }

        /// <summary>
        /// issuer	String	颁发者信息，参考12.1.12 Issuer结构
        /// </summary>
        [JsonProperty(PropertyName = "issuer")]
        public CertIssuer Issuer { get; set; }	

        /// <summary>
        /// subject	String	主体信息，参考12.1.13 Subject结构
        /// </summary>
        [JsonProperty(PropertyName = "subject")]
        public CertSubject Subject { get; set; }	

        /// <summary>
        /// extendsions	Extension[]	证书扩展值，参考12.1.9 Extension
        /// 目前只解析4个扩展值，有则返回，否则为空
        /// 深圳地标实体唯一id
        /// NETCA的实体唯一id
        /// 用户证书服务号
        /// 前证书微缩图
        /// </summary>
        [JsonProperty(PropertyName = "extendsions")]
        public List<Extension> Extendsions { get; set; }	

    }

    public class Thumbprint
    {
        /// <summary>
        /// algorithm	Int	摘要算法，参考Hash算法
        /// </summary>
        [JsonProperty(PropertyName = "algorithm")]
        public int Algorithm { get; set; }

        /// <summary>
        /// hash	String	摘要值
        /// </summary>
        [JsonProperty(PropertyName = "hash")]
        public string Hash { get; set; }

    }

    public class CertIssuer
    {
        /// <summary>
        /// issuer	String	颁发者
        /// </summary>
        [JsonProperty(PropertyName = "issuer")]
        public string Issuer { get; set; }

        /// <summary>
        /// displayName	String	颁发者的显示名字
        /// </summary>
        [JsonProperty(PropertyName = "displayName")]
        public string DisplayName { get; set; }

        /// <summary>
        /// hexEncode	String	颁发者DER编码后再Hex编码
        /// </summary>
        [JsonProperty(PropertyName = "hexEncode")]
        public string HexEncode { get; set; }

        /// <summary>
        /// c	String[]	在issuer中表示颁发者的c项
        /// </summary>
        [JsonProperty(PropertyName = "c")]
        public List<string> C { get; set; }

        /// <summary>
        /// o	String[]	在issuer中表示颁发者的o项
        /// </summary>
        [JsonProperty(PropertyName = "o")]
        public List<string> O { get; set; }

        /// <summary>
        /// ou	String[]	在issuer中表示颁发者的ou项
        /// </summary>
        [JsonProperty(PropertyName = "ou")]
        public List<string> OU { get; set; }


        /// <summary>
        /// cn	String[]	在issuer中表示颁发者的cn项
        /// </summary>
        [JsonProperty(PropertyName = "cn")]
        public List<string> CN { get; set; }

        /// <summary>
        /// email	String[]	在issuer中表示颁发者的email项
        /// </summary>
        [JsonProperty(PropertyName = "email")]
        public List<string> Email { get; set; }

        /// <summary>
        /// st	String[]	在issuer中表示颁发者的st项
        /// </summary>
        [JsonProperty(PropertyName = "st")]
        public List<string> ST { get; set; }

        /// <summary>
        /// l	String[]	在issuer中表示颁发者的l项
        /// </summary>
        [JsonProperty(PropertyName = "l")]
        public List<string> L { get; set; }

        /// <summary>
        /// dc	String[]	在issuer中表示颁发者的dc项
        /// </summary>
        [JsonProperty(PropertyName = "dc")]
        public List<string> DC { get; set; }

    }

    public class CertSubject
    {
        /// <summary>
        /// Subject	String	主体
        /// </summary>
        [JsonProperty(PropertyName = "subject")]
        public string Subject { get; set; }

        /// <summary>
        /// displayName	String	颁发者的显示名字
        /// </summary>
        [JsonProperty(PropertyName = "displayName")]
        public string DisplayName { get; set; }

        /// <summary>
        /// hexEncode	String	颁发者DER编码后再Hex编码
        /// </summary>
        [JsonProperty(PropertyName = "hexEncode")]
        public string HexEncode { get; set; }

        /// <summary>
        /// c	String[]	在issuer中表示颁发者的c项
        /// </summary>
        [JsonProperty(PropertyName = "c")]
        public List<string> C { get; set; }

        /// <summary>
        /// o	String[]	在issuer中表示颁发者的o项
        /// </summary>
        [JsonProperty(PropertyName = "o")]
        public List<string> O { get; set; }

        /// <summary>
        /// ou	String[]	在issuer中表示颁发者的ou项
        /// </summary>
        [JsonProperty(PropertyName = "ou")]
        public List<string> OU { get; set; }


        /// <summary>
        /// cn	String[]	在issuer中表示颁发者的cn项
        /// </summary>
        [JsonProperty(PropertyName = "cn")]
        public List<string> CN { get; set; }

        /// <summary>
        /// email	String[]	在issuer中表示颁发者的email项
        /// </summary>
        [JsonProperty(PropertyName = "email")]
        public List<string> Email { get; set; }

        /// <summary>
        /// st	String[]	在issuer中表示颁发者的st项
        /// </summary>
        [JsonProperty(PropertyName = "st")]
        public List<string> ST { get; set; }

        /// <summary>
        /// l	String[]	在issuer中表示颁发者的l项
        /// </summary>
        [JsonProperty(PropertyName = "l")]
        public List<string> L { get; set; }

        /// <summary>
        /// dc	String[]	在issuer中表示颁发者的dc项
        /// </summary>
        [JsonProperty(PropertyName = "dc")]
        public List<string> DC { get; set; }

    }

    public class Extension
    {
        /// <summary>
        /// oid	String	扩展oid
        /// </summary>
        [JsonProperty(PropertyName = "oid")]
        public string Oid { get; set; }

        /// <summary>
        /// value	String	Oid对应的扩展值
        /// </summary>
        [JsonProperty(PropertyName = "value")]
        public string Value { get; set; }

    }

    public class CertContentParseInfoResponse : ResponseBase<CertContentParseInfo>
    {
        public override bool IsValid { get { return null != Contents && !string.IsNullOrEmpty(Contents.ValidityStart); } }
    }

    #endregion   
  }
}
